<template>
  <view class="body">
    <u-sticky offset-top="0">
      <view class="sticky">
        <filterDropdown :menuTop="0" :filterData="filterData" :defaultSelected="defaultSelected" :updateMenuName="true" @confirm="confirm" dataFormat="Object"></filterDropdown>
      </view>
    </u-sticky>
    <view class="u-p-l-10 u-p-r-10 waterfall">
      <view class="order" v-for="(item, index) in houseList" :key="index" @click="todetail(item.id)">
        <view class="item">
          <view class="left">
            <image :src="item.faceUrl" mode="aspectFill"></image>
          </view>
          <view class="content">
            <view class="title u-line-2">
              {{item.villageName}}
              {{item.type == '整租' ? item.houseNum + item.houseHall + item.toiletNum : item.roomType}}
            </view>
            <view class="price">¥{{item.price}}/月</view>
            <view class="type">
              {{item.type}} | {{item.type == '整租' ? item.houseArea : item.roomArea}}㎡ |
              {{item.decoration}}
            </view>
          </view>
        </view>
      </view>
      <u-back-top :scroll-top="0" :top="1000"></u-back-top>
      <u-no-network></u-no-network>
    </view>
  </view>
</template>

<script>
import filterDropdown from '@/components/zy/filterDropdown.vue'
import searchData from '@/common/utils/searchData.js' //筛选菜单数据
export default {
  components: {
    filterDropdown
  },
  onLoad(option) {
    // 获取过滤数据
    this.findVillageList()
    // console.log(searchData)
    this.filterData = searchData
    this.findHouseList(option.villageName)
  },

  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      scrollTop: 0,
      houseList: [],
      loadStatus: 'loadmore',
      defaultSelected: [],
      searchData: {},
      filterData: []
    }
  },
  methods: {
    todetail(id) {
      // 跳转到 房源详情页面
      uni.navigateTo({
        url: `/pages/detail/detail?id=${id}`
      })
    },
    async findHouseList(villageName) {
      // let url = 'https://sourcebyte.vip/api/houseApi/findHouseRoomList'
      let url = `http://127.0.0.1/searchHouselist/${this.pageNum}/${this.pageSize}`
      this.searchData = { villageName }
      let [, result] = await uni.request({
        url: url,
        data: this.searchData,
        method: 'GET'
      })
      // console.log(result)
      const data = result.data.rows
      this.houseList = data
    },
    async confirm(e) {
      console.log(e)
      let villageName = e.value[0][0]
      let type = e.value[1][0]
      let housetype = type != null ? (type == 0 ? '整租' : '合租') : null
      let price = e.value[2][0]
      let combo = e.value[3]
      let houseNum = combo[0][0]
      let decoration = combo[1][0]
      let feature = combo[2][0]
      this.searchData = { villageName, housetype, price, houseNum, decoration, feature }
      // console.log('searchData', this.searchData)
      // this.findHouseList()
      // this.houseList = JSON.parse(JSON.stringify(this.houseList2))
      let [, result] = await uni.request({
        url: `http://127.0.0.1/searchHouselist/${this.pageNum}/${this.pageSize}`,
        data: this.searchData,
        method: 'GET'
      })
      const data = result.data.rows
      this.houseList = data
    },
    async findVillageList() {
      let url = 'https://sourcebyte.vip/api/houseApi/findVillageList'
      let [, result] = await uni.request({
        url: url,
        data: {
          orderByColumn: 'name',
          isAsc: 'desc'
        }
      })
      const data = result.data.rows
      // console.log(data)
      for (let i = 0; i < data.length; i++) {
        // 先转成字符串再转成对象，避免数组对象引用导致数据混乱
        let item = data[i]
        searchData[0].submenu.push({
          name: item.name,
          value: item.name
        })
      }
      // console.log(searchData)
    }
  }
}
</script>

<style lang="scss">
.body {
  height: 100vh;
}

/* 宫格,房源数据 */
.order {
  width: 710rpx;
  background-color: #ffffff;
  margin: 20rpx auto;
  border-radius: 20rpx;
  box-sizing: border-box;
  padding: 20rpx;
  font-size: 28rpx;

  .top {
    display: flex;
    justify-content: space-between;

    .left {
      display: flex;
      align-items: center;

      .store {
        // margin: 0 10rpx;
        font-size: 32rpx;
      }
    }

    .right {
      color: #2979ff;
    }
  }

  .item {
    display: flex;
    margin: 20rpx 0 0;

    .left {
      margin-right: 30rpx;

      image {
        width: 150rpx;
        height: 150rpx;
        border-radius: 10rpx;
      }
    }

    .content {
      .title {
        font-weight: bold;
        font-size: 28rpx;
        line-height: 50rpx;
      }

      .price {
        margin: 10rpx 0;
        font-size: 30rpx;
        color: #f00;
      }

      .type {
        margin: 10rpx 0;
        font-size: 24rpx;
      }
    }
  }

  .bottom {
    display: flex;
    // margin-top: 40rpx;
    padding: 0 10rpx;
    justify-content: flex-end;
    align-items: center;

    .btn {
      margin-left: 20rpx;
      line-height: 52rpx;
      width: 160rpx;
      border-radius: 26rpx;
      border: 2rpx solid #ccc;
      font-size: 26rpx;
      text-align: center;
      color: #ccc;
    }

    .evaluate {
      color: $u-tips-color;
      border-color: $u-tips-color;
    }
  }
}
</style>
